<?

//Data loading
$title='REPORT PROJECT per CUSTOMER';
$pname = GetParam("pname","");
$cname = GetParam("cname","");
//print_r($pname.' - '.$cname);
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));


else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';
$TableName = "tbljoborder";
$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = ReportList ("", "", "");
if ($Result) $pdf->ReportTable($Result);
$pdf->Output();

function ReportList ($Space, $Index, $ParentID) {
            global $RS,$Index,$TableName,$pname,$cname;
            $DBConnection   = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
            if (!$Index) $Index = 0;

$SQL = " 
		SELECT 
		 JOBORDER.jo_id,
		 JOBORDER.project_id,
		 PROJECT.project_name,
		 JOBORDER.debtur,
		 DEBTUR.CUNAME,
		 JOBORDER.jo_code,
		 JOBORDER.rat_idr as revenue,
		 JOCOST.total_amount,
		 sum(JOCOST.total_amount) as total_cost,
		 JOCOST.curr as currency
		FROM `$TableName` JOBORDER
		LEFT JOIN tblproject PROJECT
		 ON JOBORDER.project_id = PROJECT.project_id
		LEFT JOIN tbldebtur DEBTUR
		 ON JOBORDER.debtur = DEBTUR.CUCODE
		LEFT JOIN tbljoborder_costing JOCOST
		 ON JOBORDER.jo_id = JOCOST.jo_id   
          ";

$SQL .= " WHERE PROJECT.project_name like '%".$pname."%' AND DEBTUR.CUNAME like '%".$cname."%' ";
$SQL .= " GROUP BY JOBORDER.jo_code";
$SQL .= " ORDER BY DEBTUR.CUNAME ASC ";
//print_r($SQL);

          $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
          return $MyResult;
}

class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT,$pname,$cname;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        $this->SetFont('arialn','',12);
                $this->Cell(195,5,$perush,0,1,'C',0);
                $this->Cell(195,5,$title,0,1,'C',0);
                $SystemDate = date("d/m/Y");
                $this->Cell(195,5,"PROJECT NAME : ".$pname,0,1,'L',0);
                $this->Cell(195,5,"CUSTOMER NAME : ".$cname,0,1,'L',0);
                $this->Cell(195,5,$page." ".$this->PageNo(),0,1,'R',0);
                $this->Line(10,30,205,30);
                $this->Ln();
                $this->Ln();
                //Header

                //Column widths
               $w=array(10,60,25,15,20,20,20);
                $this->SetFont('arialn','',8);
                $this->Cell($w[0],7,"NO",1,0,'C',0);
                $this->Cell($w[1],7,"CUSTOMER NAME",1,0,'C',0);
                $this->Cell($w[2],7,"NO JO",1,0,'C',0);
                $this->Cell($w[3],7,"CURRENCY",1,0,'C',0);
                $this->Cell($w[4],7,"REVENUE",1,0,'C',0);
                $this->Cell($w[5],7,"COST ",1,0,'C',0);
                $this->Cell($w[6],7,"PROFIT",1,0,'C',0);
                $this->Ln();
    }

    function ReportTable($Result) {
        //print_r($Result);
        //Column widths
        $w=array(10,60,25,15,20,20,20);
        //Data
        $no = 1;
        foreach($Result as $row)
        {
			$this->Cell($w[0],6,$no++,1,0,'L',0);
			$this->Cell($w[1],6,$row["CUNAME"],1,0,'L',0);
			$this->Cell($w[2],6,$row["jo_code"],1,0,'C',0);
			$this->Cell($w[3],6,$row["currency"],1,0,'C',0);
			$this->Cell($w[4],6,FormatNumber($row["revenue"],2),1,0,'R',0);
			$this->Cell($w[5],6,FormatNumber($row["total_cost"],2),1,0,'R',0);
			$totalAmount = $row["revenue"] - $row["total_cost"];
			$this->Cell($w[6],6,FormatNumber($totalAmount,2),1,0,'R',0);
            $this->Ln();
            /*			
			$this->Cell($w[0],6,$no++,1,0,'L',0);
			$this->Cell($w[1],6,$row["jo_code"],1,0,'L',0);
			$this->Cell($w[2],6,$row["rat_idr"],1,0,'R',0);
			$this->Cell($w[3],6,$row["total_amount"],1,0,'R',0);
			$profit = ($row["rat_idr"] - $row["total_amount"]);
			$this->Cell($w[4],6,$profit,1,0,'R',0);
            $this->Ln();
            */
        }
    }

    function Footer() {
        //Closure line
        $this->Cell((10+60+25+15+20+20+20),0,'','T');
    }

}


?>
